home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.10 Oct 94 / Sprocket / Lib / AppleEventHandling.cp < prev    next >
Encoding:
Text File  |  1994-08-25  |  6.6 KB  |  236 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.      
  15.  */
  16.  
  17. #include <Types.h>
  18. #include <AppleEvents.h>
  19. #include <Errors.h>
  20. #include <Displays.h>            //    for Display Manager AppleEvent constants
  21. #include <OCEStandardMail.h>    //    for LetterSpec
  22.  
  23. #include "AppLib.h"
  24. #include "AppleEventHandling.h"
  25.  
  26. void
  27. InstallAppleEventHandlers(void)
  28.     {
  29.     //    It’s probably more efficient to use a table to install these handlers…
  30.     
  31.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  32.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenDocuments),0,false);
  33.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandlePrintDocuments),0,false);
  34.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  35.  
  36.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  37.     (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleSystemConfigNotice),0,false);
  38.     }
  39.  
  40.  
  41. OSErr
  42. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  43.     {
  44.     DescType    returnedType;
  45.     Size        actualSize;
  46.     OSErr        err;
  47.     
  48.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  49.                             &returnedType,nil,0,&actualSize);
  50.     
  51.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  52.         return noErr;                    //        everything is ok, return noErr
  53.     
  54.     if (err == noErr)                    //    We found an error attribute, so
  55.         return errAEEventNotHandled;    //        tell the client we ignored the event
  56.  
  57.     return err;                            //    Something else happened, return it
  58.     }
  59.  
  60.  
  61. OSErr
  62. ForEachDocumentInList(AEDescList documentList,EachDocumentProcPtr documentProc,void * documentParam)
  63.     {
  64.     long                documentCount,documentIndex;
  65.     DescType            returnedType;
  66.     Size                actualSize;
  67.     LetterDescriptor    theLetterDesc;
  68.     AEKeyword            keyword;
  69.     OSErr                err;
  70.  
  71.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  72.         return err;
  73.     
  74.     for (documentIndex=1; documentIndex <= documentCount; documentIndex++)
  75.         {
  76.         //    What kind of document is it?
  77.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  78.             return err;
  79.         
  80.         //    Is it an AOCE letter?
  81.         if (returnedType == typeLetterSpec)
  82.             {
  83.             //    It’s a letter
  84.             theLetterDesc.onDisk = false;
  85.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  86.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  87.             }
  88.         else
  89.             {
  90.             //    It’s just a normal document file
  91.             theLetterDesc.onDisk = true;
  92.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  93.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  94.             }
  95.             
  96.         if (err == noErr)
  97.             (*documentProc)(&theLetterDesc,documentParam);
  98.         else
  99.             break;
  100.         }
  101.     
  102.     return    err;
  103.     }
  104.  
  105.     
  106. pascal OSErr
  107. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  108.     {
  109.     OSErr    err;
  110.     
  111.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  112.         return err;
  113.  
  114.     return(OpenNewDocument());
  115.     }
  116.  
  117.  
  118. pascal OSErr
  119. HandleOpenDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  120.     {
  121.     AEDescList            documentList;
  122.     OSErr                err;
  123.     
  124.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  125.         return err;
  126.  
  127.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  128.         return err;
  129.  
  130.     err = ForEachDocumentInList(documentList,&OpenDocument,nil);
  131.     (void) AEDisposeDesc(&documentList);
  132.     return err;
  133.     }
  134.  
  135.  
  136. pascal OSErr
  137. HandlePrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  138.     {
  139.     AEDescList            documentList;
  140. #if    qUseQuickDrawGX
  141.     AEDescList            desktopPrinterList;
  142.     FSSpec                thePrinterFSSpec;
  143.     Boolean                draggedToDesktopPrinter = false;
  144. #endif
  145.     OSErr                err;
  146.     
  147.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  148.         return err;
  149.  
  150. #if    qUseQuickDrawGX
  151.     if (noErr == AEGetAttributeDesc(theAppleEvent,keyOptionalKeywordAttr,typeAEList,&desktopPrinterList))
  152.         draggedToDesktopPrinter = true;
  153. #endif
  154.  
  155.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  156.         return err;
  157.  
  158. #if    qUseQuickDrawGX
  159.     if (draggedToDesktopPrinter)
  160.         {
  161.         DescType            returnedType;
  162.         Size                actualSize;
  163.         AEKeyword            keyword;
  164.  
  165.         err = AEGetNthPtr(&desktopPrinterList,1,typeFSS,&keyword,&returnedType,
  166.                             (Ptr) &thePrinterFSSpec,sizeof(thePrinterFSSpec),&actualSize);
  167.  
  168.         (void) AEDisposeDesc(&desktopPrinterList);
  169.         }
  170.     err = ForEachDocumentInList(documentList,&PrintDocument,draggedToDesktopPrinter ? &thePrinterFSSpec : NULL);
  171.     (void) AEDisposeDesc(&documentList);
  172. #else
  173.     err = ForEachDocumentInList(documentList,&PrintDocument,nil);
  174. #endif
  175.  
  176.     return err;
  177.     }
  178.  
  179.  
  180. pascal OSErr
  181. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  182.     {
  183.     OSErr    err;
  184.     
  185.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  186.         return err;
  187.  
  188.     gDone = QuitApplication();
  189.     
  190.     if (gDone)
  191.         return noErr;
  192.     else
  193.         return userCanceledErr;
  194.     }
  195.  
  196.  
  197. ////////////////////////////////////////////////////////////////////////
  198. //
  199. //    Display Manager Suite is “Under Construction”
  200. //
  201. //    To Do: Check ERS for Display Manager events
  202.  
  203. pascal OSErr
  204. HandleSystemConfigNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  205.     {
  206.     AEDescList    displayList;
  207.     long        displayCount,displayIndex;
  208.     OSErr        err;
  209.     
  210.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  211.     
  212.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  213.         return err;
  214.     
  215.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  216.         return err;
  217.     
  218.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  219.         return err;
  220.  
  221.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  222.         {
  223.         DebugStr((StringPtr) "\pProcessing a display");
  224.         
  225.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  226.         }
  227.  
  228.  
  229.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  230.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  231.     //    can individually deal with things in a manner which they can override.
  232.  
  233.  
  234.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  235.     }
  236.